package com.jnxx.task.service.impl;

import com.jnxx.basic.service.impl.BaseServiceImpl;
import com.jnxx.basic.util.GetStaffRequestUtil;
import com.jnxx.okr.domain.Okr;
import com.jnxx.okr.mapper.OkrMapper;
import com.jnxx.okr.service.impl.OkrServiceImpl;
import com.jnxx.task.domain.Cycle;
import com.jnxx.task.domain.OkrStaff;
import com.jnxx.task.service.ICycleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class CycleServiceImpl extends BaseServiceImpl<Cycle> implements ICycleService {

    @Autowired
    private OkrMapper okrMapper;

    @Autowired
    private OkrServiceImpl okrcardService;

    @Override
    public void del(Long id) {
        // 删除周期下面的okr
        List<Long> okrIds =  okrMapper.findOkrIdsBycycleId(id);
        if (okrIds.size() > 0){
            // 遍历id，删除所有okr相关数据
            for (Long okrId : okrIds) {
                okrcardService.del(okrId);
            }
            // 删除okr
//            okrMapper.batchDel(okrIds);
        }

        // 删除周期
        super.del(id);
    }

    @Override
    public void add(Cycle cycle) {
        OkrStaff staff = GetStaffRequestUtil.getStaff();
        cycle.setStaffId(Math.toIntExact(staff.getId()));
        super.add(cycle);
    }

    @Override
    public Boolean checkCompleted(Long id) {
        Boolean delBool = false;
        OkrStaff staff = GetStaffRequestUtil.getStaff();
        List<Okr> okrList = okrMapper.findByCycleIdAndStaffId(staff.getId(), id);
        if(okrList.size() > 0 ){
            delBool = okrList.stream().allMatch(okr -> okr.getProgress() == 100);
        }
        return delBool;
    }
}
